package org.zk.dubbo_consumer.controller;


import com.github.pagehelper.PageInfo;
import lombok.Getter;
import lombok.Setter;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import org.zk.entity.Subject;
import org.zk.entity.Type;
import org.zk.service.SubjectService;
import org.zk.service.TypeService;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程类型表 前端控制器
 * </p>
 *
 * @author Alicelonely
 * @since 2019-12-11
 */
@RestController
@RequestMapping("/type")
public class TypeController {
    @Resource
    private TypeService typeService;

    @Resource
    @Getter
    @Setter
    private SubjectService subjectService;
    //获取所有的一级分类
    @ResponseBody
    @RequestMapping("/getAllType")
    public List getAllType(){
        return typeService.getAllType();
    }

    @RequestMapping("/toAllType")
    public ModelAndView toAllType(){
        return new ModelAndView("admin/classify/type");
    }


    //分页列表一级分类
    @RequestMapping("/getAllTypePages")
    @ResponseBody
    public Map getAllTypePages(String pageSize,String pageNum){
        Map map=new HashMap();
        map.put("pageSize",pageSize);
        map.put("pageNum",pageNum);
       PageInfo pageInfo= typeService.getAllTypesPage(map);
       map.put("page",pageInfo);
       map.put("pages",pageInfo.getPages());
       map.put("total",pageInfo.getTotal());
       return map;
    }
    @RequestMapping("/addType")
    public ModelAndView toAddType(){
        return new ModelAndView("admin/classify/addType");
    }
    @RequestMapping("/insertType")
    @ResponseBody
    public int insertType(String typeName){
        if(typeName.equals("")) {
            return 0;
        }else {
            typeService.addType(typeName);
            return 1;
        }
    }
    @RequestMapping("/getTypeById")
    public ModelAndView toUpdateType(int id){
        ModelAndView mv=new ModelAndView("admin/classify/updateType");
       String name= typeService.getTypeById(id);
        mv.addObject("name",name);
        mv.addObject("typeid",id);
        return mv;
    }

    @RequestMapping("/upType")
    @ResponseBody
    public int uoType(Type type){
        Map map1=new HashMap();
        map1.put("type",type);
        int result=typeService.updateType(map1);
        return result;
    }

    @RequestMapping("/delTypeSubjectCourseByTid")
    @ResponseBody
    public int delTypeSubjectCourseByTid(int id){
       List<Subject> subjectList=subjectService.getSubjectByTid(id);
       if(subjectList.size()==0){
           return 0;
       }
        int result=typeService.delTypeSubjectCourseByTid(id);
        return result;

    }



    /**
     * 获取所有一级标题
     * @return
     */
    @RequestMapping("/getType")
    public List getTypeToIndex2(){
        return typeService.getAllType();
    }

    public TypeService getTypeService() {
        return typeService;
    }

    public void setTypeService(TypeService typeService) {
        this.typeService = typeService;
    }


    //查询一级分类qds专享版
    @RequestMapping("/getShowType")
    public List getShowType(){
        return typeService.getShowType();
    }

    /**
     * 进入全部课程的入口
     * @param typeId
     * @param subjectId
     * @return
     */
    @RequestMapping("/toType")
    public ModelAndView toAddCourse(String typeId,String subjectId) {
        if(typeId==null){
            typeId="0";
        }
        if(subjectId==null){
            subjectId="0";
        }
        ModelAndView modelAndView = new ModelAndView("/index2");
        modelAndView.addObject("typeId",typeId);
        modelAndView.addObject("subjectId",subjectId);
        return modelAndView;
    }
}

